package com.example.testrxjava.fragment.project;

import android.content.Intent;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v4.app.ActivityOptionsCompat;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import com.example.testrxjava.R;
import com.example.testrxjava.activity.search.SearchActivity;
import com.example.testrxjava.adapter.TabAdapter;
import com.example.testrxjava.base.BaseFragment;
import com.example.testrxjava.util.Action;
import com.flyco.tablayout.SlidingTabLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 项目名称：TestRxjava
 *
 * @Author 郭迎辉
 * Create by 10:22 on 2019/4/18.
 * Describe:
 */
public class ProjectFragment extends BaseFragment {

    @BindView(R.id.searchTv)
    EditText mSearchTv;

    @BindView(R.id.searchIv)
    ImageView mSearchIv;

    @BindView(R.id.projectPager)
    ViewPager projectPager;
    @BindView(R.id.projectTabLayout)
    SlidingTabLayout projectTabLayout;
    private List<ProjectBean.DataBean> dataBeans;
    private TabAdapter adapter;
    private List<Fragment> mFragmentList;
    private List<String > mSecondSystemNameList;
    @Override
    protected void initData() {

        Action.getInstance().getProjectTree(new ProjectTreeSucessCallback() {
            @Override
            public void projectSucess(ProjectBean bean) {
                for (int i = 0; i < bean.getData().size(); i++) {
                    Log.e("====",bean.getData().get(i).getName());
                }
                dataBeans.addAll(bean.getData());
                addFragment();
            }

            @Override
            public void projectFail(String message) {

            }
        });


        mSearchTv.setOnClickListener(new View.OnClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
            @Override
            public void onClick(View view) {
                toSearchActivity();
            }
        });

    }

    private void addFragment() {
        mFragmentList=new ArrayList<Fragment>();
        mSecondSystemNameList=new ArrayList<String>();
        for (int i = 0; i < dataBeans.size(); i++) {
            mFragmentList.add(ProjectInfoFragment.getInstance(dataBeans.get(i).getId()));
            Log.e("project==ID",dataBeans.get(i).getId()+"");
            mSecondSystemNameList.add(dataBeans.get(i).getName());
        }
        adapter=new TabAdapter(getActivity().getSupportFragmentManager(),mFragmentList,mSecondSystemNameList);
        projectPager.setAdapter(adapter);
        projectTabLayout.setViewPager(projectPager);


    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    private void toSearchActivity() {
        Intent intent = new Intent(getActivity(), SearchActivity.class);

//        ActivityOptions options = ActivityOptions.makeSceneTransitionAnimation(MyApplication.getInstance(),
//                Pair.create(mSearchTv, getString(R.string.share_edit)),
//                Pair.create(mSearchIv, getString(R.string.share_image))
//        );
        ActivityOptionsCompat options= ActivityOptionsCompat .makeCustomAnimation(getActivity(),R.anim.anim_animo_alph_open,R.anim.anim_animo_alph_close);
        startActivity(intent, options.toBundle());
    }
    @Override
    protected View initView() {

        View view= LayoutInflater.from(getActivity()).inflate(R.layout.project_fargment,null);
        dataBeans=new ArrayList<ProjectBean.DataBean>();

        return view;
    }
}
